<?php

namespace app\api\controller;
use think\Db;
use app\data\Data;
use app\api\model\News as news_list;
use think\Request;

class Companys extends Index
{
    /**
     * 知识企业
     * 显示资源列表
     * 首页，页面显示
     * @return \think\Response
     */
    public function companys_index()
    {
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
		$where['type'] = (isset($_GET['type']) && !empty($_GET['type'])) ? $_GET['type'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();
        $where['status'] = (isset($_GET['status']) && !empty($_GET['status'])) ? $_GET['status'] : array('in',array(1,2));
        $where['time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
		if($where['type'] == 1){
			$where['name'] = array('like', "%$username%");
			$field = "id,type,name,address,introduce,internet,status";	
		}else if($where['type'] == 2){
			$where['name'] = array('like', "%$username%");
			$field = "id,name,type,star,phone,address,introduce,status";
		}else if($where['type'] == 3){
			$where['username'] = array('like', "%$username%");
			$field = "id,username,type,name,star,certificate,work,email,major,status";
		}
        
        $res['data'] = db('companys')->order('id desc')->where($where)->field($field)->limit(10)->page($page)->select();
        $res['count'] = db('companys')->where($where)->count();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '企业评级信息获取成功', $res);
        } else {
            $this->_jsonp('1000', '企业评级信息获取失败', null);
        }
    }

	
    /**
     * 知识企业详情
     */
    public function companys_msg()
    {
        $where['id'] = trim(input('get.id'));
		$type = trim(input('get.type'));
		if($type == 1){
			$field = "id,type,name,phone,address,introduce,internet";	
		}else if($type == 2){
			$field = "id,name,type,star,phone,address,introduce";
		}else if($type == 3){
			$field = "id,username,type,name,star,certificate,email,major";
		}
		
        $res = db('companys')->field($field)->where($where)->find();
		if($type == 1){
			$join[0] = "__COMPANYS_HONOR__ h";
			$join[1] = "h.id = a.honor";
			$fields = "a.years,a.levels,h.honor";
			$res['companys'] = db('companys_medal')->alias('a')->join($join[0],$join[1])->field($fields)->where('mid',$res['id'])->select();
		}
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }

    }
	
	
    /**
     * 企业管理修改状态
     */
    public function companys_update()
    {
        $id = trim(input('get.id'));
        $data['status'] = trim(input('get.status'));
		$where['id'] = explode(",", $id);
        for ($i = 0; $i < count($where['id']); $i++) {
            $res = db('companys')->where('id', $where['id'][$i])->update($data);
        }
        if ($res == true) {
            action_log('企业管理修改状态', 'log_admin', 1);
            $this->_jsonp('200', '操作成功', null);
        } else {
            action_log('企业管理修改状态', 'log_admin');
            $this->_jsonp('1000', '操作失败', null);
        }
    }
	

	 /**
     * 企业管理查询分类
     */
	public function companys_categorys(){
		$res['category'] = db('companys_category')->select();
		$res['data'] = db('companys_honor')->select();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
	}
	
	/**
     * 企业管理插入数据
     */
	public function companys_add(){
		$data['type'] = trim(input('post.type'));
		if($data['type'] == 1){
			$data['name'] = trim(input('post.name'));
			$data['phone'] = trim(input('post.phone'));
			$data['internet'] = trim(input('post.internet'));
			$data['address'] = trim(input('post.address'));
			$data['introduce'] = trim(input('post.introduce'));
			
			$auth = Request::instance()->only(['role'], 'post');
			$data1['mid'] = db('companys')->insertGetId($data);
			if($auth != null){
				foreach($auth['role'] as $value){
					$category = db('companys_honor')->where('id',$value)->find();
					$data1['honor'] = $category['id'];
					$data1['levels'] = $category['levels'];
					$data1['years'] = db('companys_category')->where('id',$category['years'])->value('name');
					$res = db('companys_medal')->insert($data1);
				}
			}
		}else if($data['type'] == 2){
			$data['name'] = trim(input('post.name'));
			$data['star'] = trim(input('post.star'));
			$data['phone'] = trim(input('post.phone'));
			$data['address'] = trim(input('post.address'));
			$data['introduce'] = trim(input('post.introduce'));
			$res = db('companys')->insert($data);
		}else if($data['type'] == 3){
			$data['username'] = trim(input('post.username'));
			$data['name'] = trim(input('post.name'));
			$data['star'] = trim(input('post.star'));
			$data['certificate'] = trim(input('post.certificate'));
			$data['email'] = trim(input('post.email'));
			$data['major'] = trim(input('post.major'));
			$res = db('companys')->insert($data);
		}
		if($res == true){
			action_log('企业管理插入数据', 'log_admin', 1);
		   $this->json('200', '插入成功', $res);
        } else {
			action_log('企业管理插入数据', 'log_admin');
            $this->json('1000', '插入失败', null);
        }
		
	}

	/**
     * 企业管理编辑详情
     */
	public function companys_edit(){
		$where['id'] = trim(input('get.id'));
		$type = trim(input('get.type'));
		if($type == 1){
			$field = "id,type,name,phone,address,introduce,internet";	
		}else if($type == 2){
			$field = "id,name,type,star,phone,address,introduce";
		}else if($type == 3){
			$field = "id,username,type,name,star,certificate,email,major";
		}
		
        $res = db('companys')->field($field)->where($where)->find();
		if($type == 1){
			$companys = db('companys_medal')->where('mid',$res['id'])->select();
			if($companys != null){
				foreach($companys as $key => $value){
					$honor[$key] = $value['honor'];
				}
				$res['honor'] = implode(',', $honor);
			}
		}
		$res['category'] = db('companys_category')->select();
		$res['data'] = db('companys_honor')->select();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
	}
	
	/**
     * 企业管理修改数据
     */
	public function companys_updates(){
		$data['type'] = trim(input('post.type'));
		$where['id'] = trim(input('post.id'));
		if($data['type'] == 1){
			$data['name'] = trim(input('post.name'));
			$data['phone'] = trim(input('post.phone'));
			$data['internet'] = trim(input('post.internet'));
			$data['address'] = trim(input('post.address'));
			$data['introduce'] = trim(input('post.introduce'));
			
			$auth = Request::instance()->only(['role'], 'post');
			$res = db('companys')->where($where)->update($data);
			db('companys_medal')->where('mid',$where['id'])->delete();
			foreach($auth['role'] as $value){
				$category = db('companys_honor')->where('id',$value)->find();
				$data1['mid'] = $where['id'];
				$data1['honor'] = $category['id'];
				$data1['levels'] = $category['levels'];
				$data1['years'] = db('companys_category')->where('id',$category['years'])->value('name');
				$res = db('companys_medal')->insert($data1);
			}
		}else if($data['type'] == 2){
			$data['name'] = trim(input('post.name'));
			$data['star'] = trim(input('post.star'));
			$data['phone'] = trim(input('post.phone'));
			$data['address'] = trim(input('post.address'));
			$data['introduce'] = trim(input('post.introduce'));
			$res = db('companys')->where($where)->update($data);
		}else if($data['type'] == 3){
			$data['username'] = trim(input('post.username'));
			$data['name'] = trim(input('post.name'));
			$data['star'] = trim(input('post.star'));
			$data['certificate'] = trim(input('post.certificate'));
			$data['email'] = trim(input('post.email'));
			$data['major'] = trim(input('post.major'));
			$res = db('companys')->where($where)->update($data);
		}
		if($res == true){
			action_log('企业管理更新数据', 'log_admin', 1);
		   $this->json('200', '更新成功', $res);
        } else {
			action_log('企业管理更新数据', 'log_admin');
            $this->json('1000', '更新失败', null);
        }
	}
	
	
	/**
     * 勋章首页
     */
	public function companys_category(){
		$page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
		
		$where['a.honor'] = array('like', "%$username%");
		$join[0] = "__COMPANYS_CATEGORY__ h";
		$join[1] = "h.id = a.years";
		$field = "a.id,a.honor,a.levels,h.name";
		$res['data'] = db('companys_honor')->alias('a')->order('id desc')->join($join[0],$join[1])->field($field)->where($where)->page($page)->limit(10)->select();
		$res['count'] = db('companys_honor')->alias('a')->join($join[0],$join[1])->where($where)->count();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
	}
	
	/**
     * 勋章删除
     */
	public function companys_companys_category_del(){
		$id = trim(input('get.id'));
		$where['id'] = explode(",", $id);
		for ($i = 0; $i < count($where['id']); $i++) {
			$res = db('companys_honor')->where('id', $where['id'][$i])->delete();
		}
		if ($res == true) {
			 action_log('勋章删除', 'log_admin', 1);
            $this->_jsonp('200', '删除成功', null);
        } else {
			 action_log('勋章删除', 'log_admin');
            $this->_jsonp('1000', '删除失败', null);
        }
	}
	
	
	/**
     * 勋章时间
     */
	public function companys_category_time(){
		$res = db('companys_category')->select();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
	}
	
	
	/**
     * 勋章新增
     */
	public function companys_category_add(){
		$data['honor'] = trim(input('get.honor'));
		$data['levels'] = trim(input('get.levels'));
		$data['years'] = trim(input('get.years'));
		$res = db('companys_honor')->insert($data);
		if ($res == true) {
			action_log('勋章插入', 'log_admin',1);
            $this->_jsonp('200', '插入成功', null);
        } else {
			action_log('勋章插入', 'log_admin');
            $this->_jsonp('1000', '插入失败', null);
        }
	}
	
	/**
     * 勋章编辑查询
     */
	public function companys_category_edit(){
		$where['id'] = trim(input('get.id'));
		$res = db('companys_honor')->where($where)->find();
		if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
	}
	
	/**
     * 勋章修改
     */
	public function companys_category_update(){
		$where['id'] = trim(input('get.id'));
		$data['honor'] = trim(input('get.honor'));
		$data['levels'] = trim(input('get.levels'));
		$data['years'] = trim(input('get.years'));
		$res = db('companys_honor')->where($where)->update($data);
		if ($res == true) {
			action_log('勋章修改', 'log_admin',1);
            $this->_jsonp('200', '插入成功', null);
        } else {
			action_log('勋章修改', 'log_admin');
            $this->_jsonp('1000', '插入失败', null);
        }
	}
	
	
	
	public function company_index(){
        $type =input('get.type');
        $where['type']=$type;
        $where['status']=1;
        if($type==1){
            $fields='id,name,phone,address';
        }else if($type==2){
            $fields='id,name,phone,star';
        }else{
            $fields='id,username,name,star';
        }
        $res=Db::name('companys')->field($fields)->where($where)->select();
        if ($res) {
            $this->_jsonp('200', '列表查询成功', $res);
        } else {
            $this->_jsonp('1000', '列表查询失败', null);
        }
    }
    public function company_detail(){
        $id =input('get.id');
        $res1=Db::name('companys_medal')->where('mid',$id)->where('static',1)->field('count(id) as num,honor')->group('honor')->select();
        $fields='name,phone,address,type,introduce,star,username,certificate,work,email,major,internet';
        $res=Db::name('companys')->where('id',$id)
            ->where('status',1)
            ->field($fields)->find();
        if($res1) {
            foreach ($res1 as $k => $y) {
                $yes = 'company' . $y['honor'];
                $res[$yes] = Db::name('companys_medal')->where('mid', $id)->where('honor', $y['honor'])->where('static', 1)->field('years')->select();
            }
        }
        if ($res) {
            $this->_jsonp('200', '详情查询成功', $res);
        } else {
            $this->_jsonp('1000', '详情查询失败', null);
        }
    }
}
